<html xmlns:py="http://genshi.edgewall.org/" py:strip="">

  <?python
    from trac.resource import Resource
  ?>

  <py:def function="sortable_th(order, desc, Class, title, href)">
    <th class="${Class}${order == Class and (desc and ' desc' or ' asc') or ''}">
      <a title="Sort by ${Class}${order == Class and not desc and ' (descending)' or ''}" href="${href}?order=${Class}&amp;desc=${(Class == order and not desc and 1 or None)}">
        $title
      </a>
    </th>
  </py:def>

  <py:def function="display_downloads(downloads, my_href)">
    <py:choose>
      <py:when test="len(downloads.downloads)">
        <div class="downloads-list">
          <table class="listing">
            <thead>
              <tr>
                <py:for each="field, field_name in downloads.visible_fields">
                  <py:choose>
                    <py:when test="field == 'id'">
                      ${sortable_th(downloads.order, downloads.desc, 'id', field_name or 'ID', my_href)}
                    </py:when>
                    <py:when test="field == 'file'">
                      ${sortable_th(downloads.order, downloads.desc, 'file', field_name or 'File', my_href)}
                    </py:when>
                    <py:when test="field == 'description'">
                      ${sortable_th(downloads.order, downloads.desc, 'description', field_name or 'Description', my_href)}
                    </py:when>
                    <py:when test="field == 'size'">
                      ${sortable_th(downloads.order, downloads.desc, 'size', field_name or 'Size', my_href)}
                    </py:when>
                    <py:when test="field == 'time'">
                      ${sortable_th(downloads.order, downloads.desc, 'time', field_name or 'Uploaded', my_href)}
                    </py:when>
                    <py:when test="field == 'count'">
                      ${sortable_th(downloads.order, downloads.desc, 'count', field_name or 'Dls', my_href)}
                    </py:when>
                    <py:when test="field == 'author'">
                      ${sortable_th(downloads.order, downloads.desc, 'author', field_name or 'Uploader', my_href)}
                    </py:when>
                    <py:when test="field == 'tags' and downloads.has_tags">
                      ${sortable_th(downloads.order, downloads.desc, 'tags', field_name or 'Tags', my_href)}
                    </py:when>
                    <py:when test="field == 'component'">
                      ${sortable_th(downloads.order, downloads.desc, 'component', field_name or 'Component', my_href)}
                    </py:when>
                    <py:when test="field == 'version'">
                      ${sortable_th(downloads.order, downloads.desc, 'version', field_name or 'Version', my_href)}
                    </py:when>
                    <py:when test="field == 'platform'">
                      ${sortable_th(downloads.order, downloads.desc, 'platform', field_name or 'Platform', my_href)}
                    </py:when>
                    <py:when test="field == 'type'">
                      ${sortable_th(downloads.order, downloads.desc, 'type', field_name or 'Type', my_href)}
                    </py:when>
                  </py:choose>
                </py:for>
              </tr>
            </thead>
            <tbody>
              <py:for each="line, download in enumerate(downloads.downloads)">
                <tr class="${line % 2 and 'even' or 'odd'}" title="${download.file}">
                  <py:for each="field, field_name in downloads.visible_fields">
                    <py:choose>
                      <py:when test="perm.has_permission('DOWNLOADS_VIEW', Resource('downloads', download.id))">
                        <py:choose>
                          <td py:when="field == 'id'" class="id">
                            <div class="id">
                              <a href="${href.downloads(download.id)}">
                                ${download.id}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'file'" class="file">
                            <div class="file">
                              <a href="${href.downloads(download.id)}">
                                ${download.file}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'description'" class="description">
                            <div class="description">
                              ${wiki_to_oneliner(context(parent), download.description)}
                            </div>
                          </td>

                          <td py:when="field == 'size'" class="size">
                            <div class="size">
                              <a href="${href.downloads(download.id)}">
                                ${pretty_size(download.size)}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'time'" class="time">
                            <div class="time">
                              <a href="${href.downloads(download.id)}">
                                ${format_datetime(download.time,"%d %b %Y")}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'count'" class="count">
                            <div class="count">
                              <a href="${href.downloads(download.id)}">
                                ${download.count or '0'}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'author'" class="author">
                            <div class="author">
                              <a href="${href.downloads(download.id)}">
                                ${download.author}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'tags' and downloads.has_tags" class="tags">
                            <div class="tags">
                              <a href="${href.downloads(download.id)}">
                                ${download.tags}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'component'" class="component">
                            <div class="component">
                              <a href="${href.downloads(download.id)}">
                                ${download.component}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'version'" class="version">
                            <div class="version">
                              <a href="${href.downloads(download.id)}">
                                ${download.version}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'platform'" class="platform">
                            <div class="platform">
                              <a href="${href.downloads(download.id)}">
                                ${download.platform.name}
                              </a>
                            </div>
                          </td>

                          <td py:when="field == 'type'" class="type">
                            <div class="type">
                              <a href="${href.downloads(download.id)}">
                                ${download.type.name}
                              </a>
                            </div>
                          </td>
                        </py:choose>
                      </py:when>
                      <py:otherwise>
                        <py:choose>
                          <td py:when="field == 'id'" class="id">
                            <div class="id">
                              ${download.id}
                            </div>
                          </td>

                          <td py:when="field == 'file'" class="file">
                            <div class="file">
                              ${download.file}
                            </div>
                          </td>

                          <td py:when="field == 'description'" class="description">
                            <div class="description">
                              ${wiki_to_oneliner(context(parent), download.description)}
                            </div>
                          </td>

                          <td py:when="field == 'size'" class="size">
                            <div class="size">
                              ${pretty_size(download.size)}
                            </div>
                          </td>

                          <td py:when="field == 'time'" class="time">
                            <div class="time">
                              ${format_datetime(download.time)}<br/>(${pretty_timedelta(download.time)} ago)
                            </div>
                          </td>

                          <td py:when="field == 'count'" class="count">
                            <div class="count">
                              ${download.count or '0'}
                            </div>
                          </td>

                          <td py:when="field == 'author'" class="author">
                            <div class="author">
                              ${download.author}
                            </div>
                          </td>

                          <td py:when="field == 'tags' and downloads.has_tags" class="tags">
                            <div class="tags">
                              ${download.tags}
                            </div>
                          </td>

                          <td py:when="field == 'component'" class="component">
                            <div class="component">
                              ${download.component}
                            </div>
                          </td>

                          <td py:when="field == 'version'" class="version">
                            <div class="version">
                              ${download.version}
                            </div>
                          </td>

                          <td py:when="field == 'platform'" class="platform">
                            <div class="platform">
                              ${download.platform.name}
                            </div>
                          </td>

                          <td py:when="field == 'type'" class="type">
                            <div class="type">
                              ${download.type.name}
                            </div>
                          </td>
                        </py:choose>
                      </py:otherwise>
                    </py:choose>
                  </py:for>
                </tr>
              </py:for>
            </tbody>
          </table>
        </div>
      </py:when>
      <py:otherwise>
        <p class="help">There are no downloads created.</p>
      </py:otherwise>
    </py:choose>
  </py:def>

</html>